package com.example.demojpa.service;

import com.example.demojpa.dao.ElectronUrlDao;
import com.example.demojpa.entity.ElectronUrl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 赵恒
 * @date 2023/4/28
 */
@Service
@Slf4j
public class ElectronUrlService {
    @Resource
    ElectronUrlDao electronUrlDao;


    public List<ElectronUrl> getElectronUrlList() {
        return electronUrlDao.findAll();
    }

    public List<ElectronUrl> getElectronUrlLike(String ip) {
        return electronUrlDao.findByUrlLike(ip);
    }

    public List<ElectronUrl> findAll() {
        return electronUrlDao.findAll();
    }

    public List<ElectronUrl> findAllBySort() {
        // 按照某个字段倒排
        Sort sort = new Sort(Sort.Direction.DESC, "id");
        return electronUrlDao.findAll(sort);
    }

    public List<ElectronUrl> findAllByPage() {
        // page 默认从0开始，1 则表示第二页
        Pageable pageable = PageRequest.of(1, 2);
        Page<ElectronUrl> page = electronUrlDao.findAll(pageable);
        int totalPages = page.getTotalPages();
        long totalElements = page.getTotalElements();
        List<ElectronUrl> electronUrlList = page.getContent();
        int number = page.getNumber();
        int size = page.getSize();
        log.info("totalPages:{}", totalPages);
        log.info("totalElements:{}", totalElements);
        log.info("number:{}", number);
        log.info("size:{}", size);
        log.info("electronUrlList:{}", electronUrlList);
        return electronUrlList;
    }

    public List<ElectronUrl> findByIdSql(Integer id) {
        return electronUrlDao.findByIdSql(id);
    }

    public List<ElectronUrl> findByTitleSql(String title) {
        return electronUrlDao.findByTitleSql(title);
    }

    public List<ElectronUrl> findByEntitySql(ElectronUrl electronUrl) {
        return electronUrlDao.findByEntitySql(electronUrl);
    }

    public List<ElectronUrl> findByIdsSql(List<Integer> idList) {
        return electronUrlDao.findByIdsSql(idList);
    }

}
